package com.zerui.manager.app.accessrecord.executer;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.zerui.basedata.client.PersonnelService;
import com.zerui.basedata.client.dto.PersonnelDTO;
import com.zerui.cola.dto.Response;
import com.zerui.cola.dto.SingleResponse;
import com.zerui.manager.accessauthdevice.AccessAuthDevice;
import com.zerui.manager.accessauthdevice.gateway.AccessAuthDeviceGateWay;
import com.zerui.manager.accessrecord.AccessRecord;
import com.zerui.manager.accessrecord.gateway.AccessRecordGateWay;
import com.zerui.manager.device.Device;
import com.zerui.manager.device.gateway.DeviceGateWay;
import com.zerui.manager.web.cmd.AccessRecordAddCmd;
import com.zerui.manager.web.enums.DevicePassTypeEnum;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @Author wh
 * @create_time 2023-09-01 9:44
 */
@Component
@CatchAndLog
public class AccessRecordAddCmdExe {

    @Resource
    private AccessRecordGateWay accessRecordGateWay;
    @Resource
    private AccessAuthDeviceGateWay accessAuthDeviceGateWay;
    @Resource
    private DeviceGateWay deviceGateWay;
    @Resource
    private PersonnelService personnelService;

    public Response execute(AccessRecordAddCmd accessRecordAddCmd) {
        //查询设备是否存在
        AccessAuthDevice accessAuthDevice = accessAuthDeviceGateWay.getByDeviceId(accessRecordAddCmd.getDeviceId());
        if(ObjectUtils.isEmpty(accessAuthDevice)){
            return Response.buildSuccess();
        }
        AccessRecord accessRecord = new AccessRecord();
        BeanUtils.copyProperties(accessRecordAddCmd,accessRecord);
        accessRecord.setDeviceCode(accessAuthDevice.getDeviceCode());
        accessRecord.setPosition(accessAuthDevice.getPosition());
        Device deviceInfo = deviceGateWay.getByDeviceId(Long.parseLong(accessRecord.getDeviceId()));
        if(ObjectUtils.isNotEmpty(deviceInfo)){
            Integer access = deviceInfo.getAccess();
            if(ObjectUtils.isNotEmpty(deviceInfo.getAccess())){
                accessRecord.setAccess(access.equals(0) ? "进" : "出");
            }
        }
        Integer mode = accessRecordAddCmd.getMode();
        if(ObjectUtils.isNotEmpty(mode)){
            String name = DevicePassTypeEnum.getNameByCode(mode);
            accessRecord.setMode(name);
        }
        if("SUCCESS".equals(accessRecord.getPassStatus())){
            accessRecord.setPassStatus("通行成功");
        }else {
            accessRecord.setPassStatus("通行失败");
        }
        String personId = accessRecord.getPersonnelId();
        if(StringUtils.isBlank(personId)){
            accessRecord.setPersonnelName("陌生人");
            accessRecord.setPersonType("陌生人");
        }else {
            if(personId.startsWith("VISITOR")){
                accessRecord.setPersonType("访客");
            }else {
                try {
                    Long aLong = Long.parseLong(personId);
                    SingleResponse<PersonnelDTO> personnel = personnelService.getPersonnel(aLong);
                    PersonnelDTO personnelDTO = personnel.getData();
                    if(ObjectUtils.isNotEmpty(personnelDTO)){
                        accessRecord.setOrgId(personnelDTO.getOrgId());
                        accessRecord.setOrgName(personnelDTO.getOrgName());
                        accessRecord.setPersonType(personnelDTO.getPersonType());
                    }
                }catch (Exception e){
                    //不处理
                }
            }
        }
        accessRecord.setCreateTime(LocalDateTime.now());
        return accessRecordGateWay.addAccessRecord(accessRecord);
    }
}
